$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
    ignore:":hidden:not(select)",
	submitHandler : function() {
		save();
	}
});
function save() {
	//上部分全部属性
	var device=new Object();
	device.deviceSn=$("#deviceSn").val();
	device.deviceType=$("#deviceType").val();
	device.remarks=$("#remarks").val();
	device.deviceState=$("#deviceState").val();
	device.locationId=$("#locationId").val();
	device.behaviorType=$("#behaviorType").val();
	var box=$("#proBox").find(" select");
	//绑定的全部属性
	var jsonArry=new Array();
	for(var i=0;i<box.length;i++){
        var json=new Object();
        if ($(box[i]).val()!=""){
            json.bindingType=$(box[i]).attr("name");
            json.deviceBindingId=$(box[i]).val();
            json.name=$(box[i]).find("option:selected").text();
            jsonArry.push(json);
		}
	}
    device.binding=jsonArry;
	$.ajax({
		type : "POST",
        contentType: "application/json;charset=utf-8",
		url : "/equipment/deviceExample/save",
		data : JSON.stringify(device),
        dataType: "json",
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});
}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
            deviceSn : {
				required : true,
                remote: {
                    url: "/equipment/deviceExample/validateByDeviceSn",     //后台处理程序
                    type: "post",               //数据发送方式
                    dataType: "json",           //接受数据格式
                    data: {                     //要传递的数据
                        deviceSn:function () {
                            return $("#deviceSn").val()
                        }
                    }
                }
			},deviceType : {
				required : true
			},deviceState : {
				required : true
			},behaviorType : {
				required : true
			},
		},
		messages : {
            deviceSn : {
				required : icon + "请输入",
                remote: icon+'设备号重复请重新选择!'
			}, deviceType : {
				required : icon + "请选择"
			}, deviceState : {
				required : icon + "请选择"
			}, behaviorType : {
				required : icon + "请选择"
			},
		}
	})
}